package com.tjb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjb.reggie.common.R;
import com.tjb.reggie.entity.Category;
import com.tjb.reggie.entity.Setmeal;
import com.tjb.reggie.entity.dto.SetmealDto;
import com.tjb.reggie.service.CategoryService;
import com.tjb.reggie.service.SetMealDishService;
import com.tjb.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/25 19:51
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    /**
     * 套餐业务
     */
    @Resource
    private SetMealService mealService;
    /**
     * 分类业务
     */
    @Resource
    private CategoryService categoryService;


    /**
     * 分页查询
     * @param page ； 页面
     * @param pageSize ： 页面大小
     * @param name ： 套餐名称
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        //先查询基本的信息，不包括分类
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like( name != null, Setmeal::getName, name);
        mealService.page(setmealPage, wrapper);
        //查询分类
        List<Category> categoryList = categoryService.list();
        //赋值封装
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        //对谈套餐名称进行处理，并且封装
        List<SetmealDto> dtoList = records.stream().map((item) -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item, dto);
            for (Category category : categoryList) {
                if (item.getCategoryId().equals(category.getId())) {
                    dto.setCategoryName(category.getName());
                    break;
                }
            }
            return dto;
        }).collect(Collectors.toList());
        //设置值
        setmealDtoPage.setRecords(dtoList);
        //返回
        return R.success(setmealDtoPage);
    }
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        mealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> stopByIds(@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(0);
            mealService.updateById(setmeal);
        }
        return R.success("停售成功");
    }
    /**
     * 起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> startByIds(@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(1);
            mealService.updateById(setmeal);
        }
        return R.success("起售成功");
    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true) // 删除这个分类下面的所有数据
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        mealService.deleteByIdsWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据分类的id，获取分类下面的套餐
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null , Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByAsc(Setmeal::getCreateTime);
        List<Setmeal> list = mealService.list(wrapper);
        return R.success(list);
    }
}
